package com.zeng.sys.authmd.restfulmodule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecommon.util.ResponseUtil;
import com.zeng.nicecore.validation.ValidGroup;
import com.zeng.sys.authmd.apimodule.service.PermissionService;
import com.zeng.sys.authmd.apimodule.service.com.zeng.sys.authmd.apimodule.constant.PermissionType;
import com.zeng.sys.authmd.apimodule.vo.PermissionVo;
import com.zeng.sys.authmd.apimodule.vo.RoleVo;
import com.zeng.sys.authmd.servicemodule.entity.Permission;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/api")
@RestController
@Slf4j
public class ApiController {
    @Autowired
    PermissionService permissionService;

    @RequestMapping("add")
    public Object add(@Validated({ValidGroup.add.class}) PermissionVo permissionVo) {
        log.debug("正在插入api资源[{}]", permissionVo);
        permissionVo.setType(PermissionType.API);
        PermissionVo insertPermission = permissionService.addOne(permissionVo);

        return ResponseUtil.success("成功响应插入").addData("permission", insertPermission);

    }

    @RequestMapping("/one")
    public Object one(String code) {
        PermissionVo oneByCode = this.permissionService.getOneByCode(code);
        return ResponseUtil.success("操作成功").addData("one", oneByCode);
    }

    @RequestMapping("list")
    public Object list(PermissionVo permissionVo, Page page) {
        log.debug("开始查询接口列表");
        permissionVo.setType(PermissionType.API);
        List<PermissionVo> permissionVos = this.permissionService.findPage(page, permissionVo);

        return ResponseUtil.success("操作成功").addData("apiList", permissionVos).addData("page", page);
    }
    @RequestMapping("all")
    public Object all(PermissionVo permissionVo) {
        log.debug("开始查询接口列表");
        permissionVo.setType(PermissionType.API);
        List<PermissionVo> permissionVos = this.permissionService.findAll(permissionVo);
        return ResponseUtil.success("操作成功").addData("apiList", permissionVos);
    }
    @RequestMapping("update")
    public Object update(PermissionVo permissionVo) {
        return ResponseUtil.success("更新成功,更新的数据条数:" + this.permissionService.update(permissionVo));
    }

    @RequestMapping("delete")
    public Object delete(String code) {
        return ResponseUtil.success("删除数据成功,影响数据条数:" + this.permissionService.delete(code));
    }

    @RequestMapping("batchDelete")
    public Object batchDelete(@RequestBody List<String> codes) {
        return ResponseUtil.success("删除数据成功,影响数据条数:" + this.permissionService.batchDelete(codes));
    }


}
